package com.yu.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils;
import com.baomidou.mybatisplus.extension.service.IService;
import com.yu.domain.loginUser.dto.User;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 第二种excel导入方法
 */
public class ExcelListener extends AnalysisEventListener {

    private final IService service;
    private static final int BATCH_COUNT = 100;

    private List<Long> resultList = new ArrayList<>();

    public ExcelListener(IService service) {
        this.service = service;
    }

    public List<Long> getResultList() {
        return resultList;
    }
    public void setResultList(List<Long> resultList) {
        this.resultList = resultList;
    }
    /**
     * 缓存的数据
     */
    private List<User> cachedDataList = ListUtils.newArrayListWithExpectedSize(ExcelListener.BATCH_COUNT);
    private List<User> insertList = ListUtils.newArrayListWithExpectedSize(ExcelListener.BATCH_COUNT);

    private final AtomicInteger count = new AtomicInteger(0);

    @Override
    public void invoke(Object o, AnalysisContext analysisContext) {
//        if (ObjectUtil.isNull(entity) || (StrUtil.isBlank(entity.getEnumCode()) && StrUtil.isBlank(entity.getEnumName()) && StrUtil.isBlank(entity.getEnumDict()))) {
//            return;
//        }
//        count.addAndGet(1);
//        EnumImportModelListener.log.info("解析到一条数据:{}", JSON.toJSONString(entity));
//        DmEnumDictModelDo commandDO = BeanUtil.copyProperties(entity, DmEnumDictModelDo.class);
//        cachedDataList.add(commandDO);
//
//        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
//        if (cachedDataList.size() >= EnumImportModelListener.BATCH_COUNT) {
//            batchInsert();
//            // 存储完成清理 list
//            cachedDataList = ListUtils.newArrayListWithExpectedSize(EnumImportModelListener.BATCH_COUNT);
//        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
//        batchInsert();
//        EnumImportModelListener.log.info("所有数据解析完成！");
    }

//    @Async
//    public void batchInsert() {
//        getDataList();
//        EnumImportModelListener.log.info("{}条数据，开始存储数据库!", count.get());
//        service.saveBatch(insertList, EnumImportModelListener.BATCH_COUNT);
//        List<Long> ids = insertList.stream().map(DmEnumDictModelDo::getId).collect(Collectors.toList());
//        resultList.addAll(ids);
//        // 存储完成清理 list
//        insertList = ListUtils.newArrayListWithExpectedSize(EnumImportModelListener.BATCH_COUNT);
//        EnumImportModelListener.log.info("存储数据库成功!");
//    }
//
//    private void getDataList() {
//        for (int i = 0; i < cachedDataList.size(); i++) {
//            DmEnumDictModelDo amDo = new DmEnumDictModelDo();
//            BeanUtil.copyProperties(cachedDataList.get(i), amDo);
//            amDo.setEnumDict(enumDict);
//            insertList.add(amDo);
//        }
//    }
}
